﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;
    using WMS6.Web.OrderMgtServiceRef;

    public class FlowThruOrder : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkApportion;
        protected CheckBox chkIsActive;
        protected DropDownList ddlStatus;
        protected HtmlForm F79561;
        protected GridView gvXorderDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnImportPOValue;
        protected HtmlInputHidden hdnInportValue;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage imgConsigneeID;
        protected Label lblConsigneeID;
        protected Label lblDeliveryDate;
        protected Label lblDoor;
        protected Label lblExternalLoadID;
        protected Label lblExternOrderID;
        protected Label lblLoadID;
        protected Label lblMsg;
        protected Label lblOrderID;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblPoID;
        protected Label lblRoute;
        protected Label lblStatus;
        protected Label lblStop;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtConsigneeID;
        protected TextBox txtDeliveryDate;
        protected TextBox txtDoor;
        protected TextBox txtExternalLoadID;
        protected TextBox txtExternOrderID;
        protected TextBox txtLoadID;
        protected TextBox txtOrderID;
        protected TextBox txtOwnerID;
        protected TextBox txtPoID;
        protected TextBox txtRoute;
        protected TextBox txtStop;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("FlowThruOrderDetail.aspx?from=edit", false);
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = "状态不等于新建无法删除!";
                }
                else
                {
                    List<string> gInitLocal0 = new List<string> {
                        this.txtOrderID.Text
                    };
                    List<string> selectedID = gInitLocal0;
                    string strtemp = CrossdockServiceClientProxy.GetInstance().DeleteXordersInfo(selectedID);
                    this.lblMsg.Text = strtemp + "删除成功!";
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "删除失败:" + ex.Message;
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "明细删除成功！";
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("FlowThruOrderList.aspx?from=edit", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text))
            {
                this.lblMsg.Text = "货主不能为空!";
            }
            else if (string.IsNullOrEmpty(this.txtPoID.Text))
            {
                this.lblMsg.Text = "采购订单不能为空!";
            }
            else if (string.IsNullOrEmpty(this.txtConsigneeID.Text))
            {
                this.lblMsg.Text = "客户不能为空!";
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateXorderInfo();
            }
            else
            {
                this.InsertXorderInfo();
            }
        }

        protected string FormatFlowThroUrl(string url, string lineID)
        {
            return string.Format(url, this.hdnFrom.Value.Trim(), this.txtOrderID.Text, lineID);
        }

        protected void gvXorderDetail_Init(object sender, EventArgs e)
        {
        }

        protected void gvXorderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvXorderDetail.PageIndex * this.gvXorderDetail.PageSize;
                e.Row.Cells[2].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvXorderDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
        }

        private void InsertXorderInfo()
        {
            try
            {
                XordersInfo xOrderInfo = base.DataEntityViewState as XordersInfo;
                xOrderInfo.OrderID = this.txtOrderID.Text.Trim();
                xOrderInfo.Status = this.ddlStatus.SelectedValue;
                xOrderInfo.OrderType = "FlowThru";
                xOrderInfo.Door = this.txtDoor.Text.Trim();
                xOrderInfo.OwnerID = this.txtOwnerID.Text.Trim();
                xOrderInfo.ConsigneeID = this.txtConsigneeID.Text.Trim();
                xOrderInfo.Route = this.txtRoute.Text.Trim();
                xOrderInfo.DeliveryDate = string.IsNullOrEmpty(this.txtDeliveryDate.Text.Trim()) ? DateTime.Now : Convert.ToDateTime(this.txtDeliveryDate.Text.Trim());
                xOrderInfo.PoID = this.txtPoID.Text.Trim();
                xOrderInfo.ExternOrderID = this.txtExternOrderID.Text.Trim();
                xOrderInfo.Stop = this.txtStop.Text.Trim();
                xOrderInfo.ExternalLoadID = this.txtExternalLoadID.Text.Trim();
                xOrderInfo.LoadID = this.txtLoadID.Text.Trim();
                xOrderInfo.VehicleCarrier = "";
                xOrderInfo.Apportion = "";
                xOrderInfo.IsActive = "";
                xOrderInfo.UpdatedBy = "";
                xOrderInfo.CreatedBy = base.UserInfo.UserID;
                string strTemp = CrossdockServiceClientProxy.GetInstance().InsertXordersInfo(xOrderInfo);
                this.txtOrderID.Text = strTemp;
                this.txtOrderID.Enabled = false;
                this.lblMsg.Text = strTemp + "更新成功！";
                base.DataEntityViewState = xOrderInfo;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "添加失败" + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "ReceiptStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnOrderID.Value = base.Request.QueryString["ID"].ToString();
                this.SetXordersInfo2Form();
            }
            else
            {
                this.gvXorderDetail.Columns[1].Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetXordersInfo2Form()
        {
            try
            {
                string orderID = this.hdnOrderID.Value;
                XordersInfo xOrderInfo = CrossdockServiceClientProxy.GetInstance().GetXordersInfoByID(orderID);
                base.DataEntityViewState = xOrderInfo;
                if (xOrderInfo != null)
                {
                    this.txtOrderID.Text = orderID;
                    this.ddlStatus.SelectedValue = xOrderInfo.Status;
                    this.txtDoor.Text = xOrderInfo.Door;
                    this.txtOwnerID.Text = xOrderInfo.OwnerID;
                    this.txtConsigneeID.Text = xOrderInfo.ConsigneeID;
                    this.txtRoute.Text = xOrderInfo.Route;
                    this.txtDeliveryDate.Text = base.GetPreferenceShortDate(xOrderInfo.DeliveryDate);
                    this.txtPoID.Text = xOrderInfo.PoID;
                    this.txtExternOrderID.Text = xOrderInfo.ExternOrderID;
                    this.txtStop.Text = xOrderInfo.Stop;
                    this.txtExternalLoadID.Text = xOrderInfo.ExternalLoadID;
                    this.txtLoadID.Text = xOrderInfo.LoadID;
                    this.txtOrderID.Enabled = false;
                    this.chkApportion.Checked = xOrderInfo.Apportion == "Y";
                    this.chkIsActive.Checked = xOrderInfo.IsActive == "Y";
                    base.BindData2GridView(this.gvXorderDetail, xOrderInfo.XOrderDetailInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void txtPoID_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtPoID.Text))
            {
                try
                {
                    PurchaseOrderInfo purchaseOrderInfo = OrderMgtServiceClientProxy.GetInstance().GetPurchaseOrderInfoByPoID(this.txtPoID.Text);
                    if (purchaseOrderInfo != null)
                    {
                        this.txtOwnerID.Text = purchaseOrderInfo.OwnerID;
                        this.txtExternOrderID.Text = purchaseOrderInfo.ExternalPoID;
                        if (purchaseOrderInfo.PurchaseOrderDetailInfos.Count > 0)
                        {
                            XordersInfo xordersInfo = new XordersInfo {
                                XOrderDetailInfos = new List<XorderDetailInfo>()
                            };
                            foreach (PurchaseOrderDetailInfo info in purchaseOrderInfo.PurchaseOrderDetailInfos)
                            {
                                XorderDetailInfo xorderDetailInfo = new XorderDetailInfo {
                                    LineID = info.LineID,
                                    OwnerID = info.OwnerID,
                                    SkuID = info.SkuID,
                                    OriginalQty = info.OrderedQty,
                                    ShippedQty = 0M,
                                    OpenQty = 0M,
                                    ProcessedQty = 0M,
                                    Status = "New",
                                    ToProcessQty = 0M,
                                    Sequence = 0M,
                                    ConvertedQty = 0M
                                };
                                xordersInfo.XOrderDetailInfos.Add(xorderDetailInfo);
                            }
                            base.DataEntityViewState = xordersInfo;
                            base.BindData2GridView(this.gvXorderDetail, xordersInfo.XOrderDetailInfos);
                            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2);</script>", false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        private void UpdateXorderInfo()
        {
            try
            {
                XordersInfo xOrderInfo = base.DataEntityViewState as XordersInfo;
                xOrderInfo.OrderID = this.txtOrderID.Text.Trim();
                xOrderInfo.Status = this.ddlStatus.SelectedValue;
                xOrderInfo.Door = this.txtDoor.Text.Trim();
                xOrderInfo.OrderType = "FlowThru";
                xOrderInfo.OwnerID = this.txtOwnerID.Text.Trim();
                xOrderInfo.ConsigneeID = this.txtConsigneeID.Text.Trim();
                xOrderInfo.Route = this.txtRoute.Text.Trim();
                xOrderInfo.DeliveryDate = string.IsNullOrEmpty(this.txtDeliveryDate.Text.Trim()) ? DateTime.Now : Convert.ToDateTime(this.txtDeliveryDate.Text.Trim());
                xOrderInfo.PoID = this.txtPoID.Text.Trim();
                xOrderInfo.ExternOrderID = this.txtExternOrderID.Text.Trim();
                xOrderInfo.Stop = this.txtStop.Text.Trim();
                xOrderInfo.ExternalLoadID = this.txtExternalLoadID.Text.Trim();
                xOrderInfo.LoadID = this.txtLoadID.Text.Trim();
                xOrderInfo.WhID = "";
                xOrderInfo.OrderType = "";
                xOrderInfo.VehicleCarrier = "";
                xOrderInfo.Apportion = this.chkApportion.Checked ? "Y" : "N";
                xOrderInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                xOrderInfo.CreatedBy = base.UserInfo.UserID;
                xOrderInfo.UpdatedBy = base.UserInfo.UserID;
                string strTemp = CrossdockServiceClientProxy.GetInstance().UpdateXordersInfo(xOrderInfo);
                this.lblMsg.Text = strTemp + "保存成功！";
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "更新失败" + ex.Message;
            }
        }
    }
}

